package tv.huan.media.rabbit.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tv.huan.media.domain.Album;
import tv.huan.media.mail.MailUtil;
import tv.huan.media.rabbit.RabbitConstant;
import tv.huan.media.util.MailInfoUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Component
//@RabbitListener(containerFactory = "pointTaskContainerFactory", queues = RabbitConstant.EMAIL_QUEUE)  指定单独的监听方式
//@RabbitListener(containerFactory = "pointTaskContainerFactory", queues = RabbitConstant.EMAIL_QUEUE)
@RabbitListener(queues = RabbitConstant.EMAIL_QUEUE)
public class EmailConsumer {

    private static final Logger log = LoggerFactory.getLogger(EmailConsumer.class);

    @Autowired
    private MailUtil mailUtil;

    /**
     * 接收方邮箱
     */
    @Value("${spring.mail.to}")
    private String to;

    @RabbitHandler
    public void process(String json, Channel channel, Message message)throws IOException {
        try {
            JSONObject jsonObject = JSON.parseObject(json);
            String type = jsonObject.getString("type");
            String subject = jsonObject.getString("subject");
            //log邮件主题:::::参数
            log.info(subject + "::::" + json);
            String text = jsonObject.getString("text");
            JSONArray jsonArray = jsonObject.getJSONArray("array");
            List<Album> list = jsonArray.toJavaList(Album.class);

            StringBuilder content = new StringBuilder("<html><head></head><body>" + text);
            content.append("<table border=\"5\" style=\"border:solid 1px #E8F2F9;font-size=14px;;font-size:18px;\">");
            content.append("<tr style=\"background-color: #F0F0F0; color:#000000\"><th>媒资ID</th><th>片名</th><th>类型</th><th>授权方</th>");
            //版权到期提醒任务、推荐、下线通知增加版权时间两列
            if (type.equals(MailInfoUtil.COPYRIGHT_WARN) || type.equals(MailInfoUtil.RECOMEND) || type.equals(MailInfoUtil.OFF_LINE_NOTIFY)){
                content.append("<th>版权开始时间</th><th>版权结束时间</th>");
            }
            content.append("</tr>");
            for (Album album : list) {
                content.append("<tr>");
                content.append("<td>" + album.getAlbumId() + "</td>"); //第一列
                content.append("<td>" + album.getTitle() + "</td>"); //第二列
                content.append("<td>" + album.getClassName() + "</td>"); //第三列
                content.append("<td>" + album.getAuthorizerName() + "</td>"); //第四列
                //版权到期提醒任务、推荐、下线通知增加版权时间两列
                if (type.equals(MailInfoUtil.COPYRIGHT_WARN) || type.equals(MailInfoUtil.RECOMEND) || type.equals(MailInfoUtil.OFF_LINE_NOTIFY)){
                    content.append("<td>" + album.getAuthorBeginDate() + "</td>"); //第五列
                    content.append("<td>" + album.getAuthorEndDate() + "</td>"); //第六列
                }
                content.append("</tr>");
            }
            content.append("</table>");
//        content.append("<h3>description</h3>");
            content.append("</body></html>");
            log.info("邮件接收人：" + to);
            String[] toArray = to.split(",");
            mailUtil.sendMailHtml(subject,content.toString(),toArray);
            //消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //失败后消息被确认
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            log.error("mq接收或处理消息失败",e);
        }
    }

    public static void main(String[] args) {
        String s = "yuguohui@huan.tv,shaoxiujie@huan.tv,wangtian@huan.tv";
        String[] a = s.split(",");
        System.out.println(a.length);
    }
}
